
$(function(){
	getMessageByKeyword();
});

var Search ={
		classlist :null,
		topiclist :null,
		topicArray:new Array(),
		classArray:new Array(),
		voteArray:new Array(),
		searchArray:new Array(),
		activityArray:new Array(),
		 topic_cnt:0,
		 class_cnt:0,
		 vote_cnt:0,
		 surver_cnt:0,
		 activities_cnt:0
};
 

function showClass(){
	$('#content').html('');
	var left_result='' ;
	if(Search.classArray!=''&&Search.classArray!=undefined){
	for(var i=0;i<Search.classArray.length;i++){
		var cover_pic='';
		//封面图片是否存在，若还不存在则使用默认图片
		if(Search.classArray[i].pic ==''){
			cover_pic="images/nopic.jpg";
		}else{
			cover_pic=Search.classArray[i].pic;
		}
		left_result+=' <div class="row">';
		left_result+=' <div class="ibox">';
	    left_result+='<div class="ibox-content">';
        left_result+='<h2>'+Search.classArray[i].name+'</h2>';
        left_result+='  <div class="row">';
        left_result+='<div class="col-md-3" > <img alt="image" class="img-responsive" src="'+cover_pic+'"></div>';
    	left_result+='	<div class="col-md-9" >';
    	left_result+='	<p>'+Search.classArray[i].indroduce+'</p>';
	    left_result+='<p>';
     	left_result+='   <a href="web/classDetail.jsp?class_id='+Search.classArray[i].class_id+'">进入班组 ></a>';//共<a>188</a>篇话题 
     	left_result+='</p>';
     	left_result+='</div>';
        left_result+=' </div>';
        left_result+=' <div class="row m-t-sm ">';
        left_result+=' </div>';
        left_result+=' </div>';
        left_result+=' </div>';
	    left_result+='  </div>  ';    
	}
	
	$('#content').append(left_result);
	if(Search.class_cnt>$('#content').children().length){
		$('#content').append('<button class="btn btn-primary btn-block m " id="load_class_data_button" onclick="loadClassData( )"><i class="fa fa-arrow-down"></i> 显示更多</button>');
	}else{
		$('#content').append('<button class="btn btn-primary btn-block m"   > 没有更多班组信息</button>');
	}
	
	}
	
	
}
function showTopic(){
	$('#content').html('');
	var result='';
	if(Search.topicArray!=''&&Search.topicArray!=undefined){
	for(var i=0;i<Search.topicArray.length;i++){
		var cover_pic='';
		//封面图片是否存在，不存在取pic_list第一张，若还不存在则使用默认图片
		if(Search.topicArray[i].cover_pic==''){
			 cover_pic="images/nopic.jpg";
		}else{
			cover_pic=Search.topicArray[i].cover_pic;
		}			
		result+='<div class="ibox-content">';
		result+='<a javascript:void(0) onclick="Common.openDetailPage('+Search.topicArray[i].topic_id+','+Search.topicArray[i].class_id+','+Search.topicArray[i].type+')" class="btn-link">';
        result+='<h2>'+Search.topicArray[i].title+'</h2>';
   		result+='</a>';
        result+='<div class="small m-b-xs text-right ">';
        result+='<span class="text-muted"><i class="fa fa-clock-o"></i>'+Search.topicArray[i].create_time+'</span> ';
        result+='</div>';
        result+='<div class="row">';
        result+='<div class="col-md-3" ><img  class="img-responsive" src="'+cover_pic+'"></div>';
        result+='<div class="col-md-9" ><p>'+Search.topicArray[i].indroduce+'</p> </div>';
        result+='</div>';
        result+='<div class="row m-t-sm ">';
        result+='<div class="col-md-12">';
        result+='<div class="col-md-2 text-center">  ';
        result+='<button class="btn btn-success btn-xs" type="button">'+Search.topicArray[i].class_name+'</button>';
        result+='</div>';
        result+='<div class="col-md-2 text-center"><a javascript:void(0) onclick="Common.openDetailPage('+Search.topicArray[i].topic_id+','+Search.topicArray[i].class_id+','+Search.topicArray[i].type+')" class="btn-link">';
        result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button">';
        result+='<i class="fa fa-external-link">'+Search.topicArray[i].forward_cnt+'转发 </i>';
        result+='</button></a></div>';
        result+='<div class="col-md-2 text-center "><a javascript:void(0) onclick="Common.openDetailPage('+Search.topicArray[i].topic_id+','+Search.topicArray[i].class_id+','+Search.topicArray[i].type+')" class="btn-link">';
        result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button" > ';
        result+='<i class="fa fa-commenting-o">'+Search.topicArray[i].comment_cnt+'评论</i>   ';
        result+='</button></a></div>';
        result+='<div class="col-md-2 text-center " >'; 
        if(Search.topicArray[i].is_thumbs==0){//用户已经点赞
//        	result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button"><i class="fa fa-thumbs-o-up" style="color:red">'+Search.topicArray[i].thumbs_cnt+'点赞 </i>  '; 
//        }else{//用户还未点赞
//        	result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button" onclick="topicthumbsUp('+Search.topicArray[i].topic_id+',2)" ><i class="fa fa-thumbs-o-up"   >'+Search.topicArray[i].thumbs_cnt+'点赞 </i> ';
//        }
         	result+='<button id="zan_'+Search.topicArray[i].topic_id+'" class="btn btn-white btn-xs" type="button"  onclick="topicthumbsUp('+Search.topicArray[i].topic_id+',10)"><i class="fa fa-thumbs-o-up" style="color:red"   id="'+Search.topicArray[i].topic_id+'_a" >'+Search.topicArray[i].thumbs_cnt+'点赞 </i>  '; 
         }else{//用户还未点赞
         	result+='<button id="zan_'+Search.topicArray[i].topic_id+'" class="btn btn-white btn-xs" type="button" onclick="topicthumbsUp('+Search.topicArray[i].topic_id+',2)" ><i class="fa fa-thumbs-o-up"  id="'+Search.topicArray[i].topic_id+'_a" >'+Search.topicArray[i].thumbs_cnt+'点赞 </i> ';
         }
        
        result+='</button></div>';
        result+='</div>';
        result+='</div>';
        result+='</div>';
	}
	$('#content').append(result);
	
		var result1='';
		if(Search.topic_cnt>$('#content').children().length){
			result1+='<button class="btn btn-primary btn-block m " id="load_topic_data_button" onclick="loadTopicData()"><i class="fa fa-arrow-down"></i> 显示更多</button>';
		}else{
			result1+='<button class="btn btn-primary btn-block m"> 没有更多消息</button>';
		}
		$('#content').append(result1);
	}
}
  

//加载更多话题
function loadTopicData (){
	$('#load_topic_data_button').remove();
	 $.ajax({
			type : 'POST',
			dataType:'json', 
			url : ctp + '/interface/keyWordSearchMoreTopics',
			data: {
				keyWord:$('#search_key').val()  ,
				type:0,
				pos_idx:$('#content').children().length,
				count:15
			},
			success:function(data){
				var result='';
				if(data.data!=''&&data.data!=undefined){
				for(var i=0;i<data.data.length;i++){
					Search.topicArray.push(data.data[i] );
					var cover_pic='';
					//封面图片是否存在，不存在取pic_list第一张，若还不存在则使用默认图片
					if(data.data[i].cover_pic==''){
						 cover_pic="images/nopic.jpg";
					}else{
						cover_pic=data.data[i].cover_pic;
					}			
					result+='<div class="ibox-content">';
					result+='<a javascript:void(0) onclick="Common.openDetailPage('+data.data[i].topic_id+','+data.data[i].class_id+','+data.data[i].type+')" class="btn-link">';
			        result+='<h2>'+data.data[i].title+'</h2>';
			   		result+='</a>';
			        result+='<div class="small m-b-xs text-right ">';
			        result+='<span class="text-muted"><i class="fa fa-clock-o"></i>'+data.data[i].create_time+'</span> ';
			        result+='</div>';
			        result+='<div class="row">';
			        result+='<div class="col-md-3" ><img  class="img-responsive" src="'+cover_pic+'"></div>';
			        result+='<div class="col-md-9" ><p>'+data.data[i].indroduce+'</p> </div>';
			        result+='</div>';
			        result+='<div class="row m-t-sm ">';
			        result+='<div class="col-md-12">';
			        result+='<div class="col-md-2 text-center">  ';
			        result+='<button class="btn btn-success btn-xs" type="button">'+data.data[i].class_name+'</button>';
			        result+='</div>';
			        result+='<div class="col-md-2 text-center"><a javascript:void(0) onclick="Common.openDetailPage('+data.data[i].topic_id+','+data.data[i].class_id+','+data.data[i].type+')" class="btn-link">';
			        result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button">';
			        result+='<i class="fa fa-external-link">'+data.data[i].forward_cnt+'转发 </i>';
			        result+='</button></a></div>';
			        result+='<div class="col-md-2 text-center "><a javascript:void(0) onclick="Common.openDetailPage('+data.data[i].topic_id+','+data.data[i].class_id+','+data.data[i].type+')" class="btn-link">';
			        result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button" > ';
			        result+='<i class="fa fa-commenting-o">'+data.data[i].comment_cnt+'评论</i>   ';
			        result+='</button></a></div>';
			        result+='<div class="col-md-2 text-center " >'; 

			        if(data.data[i].is_thumbs==0){//用户已经点赞
                    	result+='<button id="zan_'+data.data[i].topic_id+'" class="btn btn-white btn-xs" type="button"  onclick="topicthumbsUp('+data.data[i].topic_id+',10)"><i class="fa fa-thumbs-o-up" style="color:red"   id="'+data.data[i].topic_id+'_a" >'+data.data[i].thumbs_cnt+'点赞 </i>  '; 
                    }else{//用户还未点赞
                    	result+='<button id="zan_'+data.data[i].topic_id+'" class="btn btn-white btn-xs" type="button" onclick="topicthumbsUp('+data.data[i].topic_id+',2)" ><i class="fa fa-thumbs-o-up"  id="'+data.data[i].topic_id+'_a" >'+data.data[i].thumbs_cnt+'点赞 </i> ';
                    }
			        result+='</button></div>';
			        result+='</div>';
			        result+='</div>';
			        result+='</div>';
				}
				$('#content').append(result);
				
					var result1='';
					if(Search.topic_cnt>$('#content').children().length){
						result1+='<button class="btn btn-primary btn-block m " id="load_topic_data_button" onclick="loadTopicData()"><i class="fa fa-arrow-down"></i> 显示更多</button>';
					}else{
						result1+='<button class="btn btn-primary btn-block m"> 没有更多消息</button>';
					}
					$('#content').append(result1);
				}
			},
			error:function(data){
			}
	    });
}

//加载更多班组信息
function loadClassData(){
	$('#load_class_data_button').remove();
	 $.ajax({
			type : 'POST',
			dataType:'json', 
			url : ctp + '/interface/keyWordSearchMoreClass',
			data: {
				keyWord:$('#search_key').val()  ,
				pos_idx:$('#content').children().length,
				count:15
			},
			success:function(data){
				console.log(JSON.stringify(data));
				var left_result="";
				if(data.data!=''&&data.data!=undefined){
					for(var i=0;i<data.data.length;i++){
						Search.classArray.push(data.data[i]);
						var cover_pic='';
						//封面图片是否存在，若还不存在则使用默认图片
						if(data.data[i].pic ==''){
							cover_pic="images/nopic.jpg";
						}else{
							cover_pic=data.data[i].pic;
						}
						left_result+=' <div class="row">';
						left_result+=' <div class="ibox">';
					    left_result+='<div class="ibox-content">';
				        left_result+='<h2>'+data.data[i].name+'</h2>';
				        left_result+='  <div class="row">';
				        left_result+='<div class="col-md-3" > <img alt="image" class="img-responsive" src="'+cover_pic+'"></div>';
				    	left_result+='	<div class="col-md-9" >';
				    	left_result+='	<p>'+data.data[i].indroduce+'</p>';
					    left_result+='<p>';
				     	left_result+='   <a href="web/classDetail.jsp?class_id='+data.data[i].class_id+'">进入班组 ></a>';//共<a>188</a>篇话题 
				     	left_result+='</p>';
				     	left_result+='</div>';
				        left_result+=' </div>';
				        left_result+=' <div class="row m-t-sm ">';
				        left_result+=' </div>';
				        left_result+=' </div>';
				        left_result+=' </div>';
					    left_result+='  </div>  ';    
					}
					
					$('#content').append(left_result);
					if(Search.class_cnt>$('#content').children().length){
						$('#content').append('<button class="btn btn-primary btn-block m " id="load_class_data_button" onclick="loadClassData( )"><i class="fa fa-arrow-down"></i> 显示更多</button>');
					}else{
						$('#content').append('<button class="btn btn-primary btn-block m"   > 没有更多班组信息</button>');
					}
				////////
				}
			},
			error:function(data){
			}
	    });
}



//关键字搜索班组
function getMessageByKeyword(){
	Search.topicArray=new Array();
	Search.voteArray=new Array();
	Search.searchArray=new Array();
	Search.activityArray=new Array();
	 $.ajax({
			type : 'POST',
			dataType:'json', 
			url : ctp + '/interface/keyWordSearch',
			data: {
				keyWord:$('#search_key').val()  ,
				source:1//0 手机 1 电脑
			},
			success:function(data){
				Search.classArray=data.data.class_list;
				Search.topicArray=data.data.topic_list
				Search.topic_cnt=data.topic_cnt;
				Search.class_cnt=data.class_cnt;
				Search.vote_cnt=data.vote_cnt;
				Search.surver_cnt=data.surver_cnt;
				Search.activities_cnt=data.activities_cnt;
				showTopic();
				$('#topic_cnt').html(data.topic_cnt);
				$('#class_cnt').html(data.class_cnt);
				$('#vote_cnt').html(data.vote_cnt);
				$('#search_cnt').html(data.surver_cnt);
				$('#activity_cnt').html(data.activities_cnt);
			},
			error:function(data){
			}
	    });
}


function  getTopicData(type){
	$('#get_data_button').remove();
	 $('#content').html('');
	 $.ajax({
			type : 'POST',
			dataType:'json', 
			url : ctp + '/interface/keyWordSearchMoreTopics',
			data: {
				keyWord:$('#search_key').val()  ,
				type:type,
				pos_idx:$('#content').children().length,
				count:15
			},
			success:function(data){
				var result='';
				if(data.data!=''&&data.data!=undefined){
				for(var i=0;i<data.data.length;i++){
					var cover_pic='';
					//封面图片是否存在，不存在取pic_list第一张，若还不存在则使用默认图片
					if(data.data[i].cover_pic==''){
						 cover_pic="images/nopic.jpg";
					}else{
						cover_pic=data.data[i].cover_pic;
					}			
					result+='<div class="ibox-content">';
					result+='<a javascript:void(0) onclick="Common.openDetailPage('+data.data[i].topic_id+','+data.data[i].class_id+','+data.data[i].type+')" class="btn-link">';
			        result+='<h2>'+data.data[i].title+'</h2>';
			   		result+='</a>';
			        result+='<div class="small m-b-xs text-right ">';
			        result+='<span class="text-muted"><i class="fa fa-clock-o"></i>'+data.data[i].create_time+'</span> ';
			        result+='</div>';
			        result+='<div class="row">';
			        result+='<div class="col-md-3" ><img  class="img-responsive" src="'+cover_pic+'"></div>';
			        result+='<div class="col-md-9" ><p>'+data.data[i].indroduce+'</p> </div>';
			        result+='</div>';
			        result+='<div class="row m-t-sm ">';
			        result+='<div class="col-md-12">';
			        result+='<div class="col-md-2 text-center">  ';
			        result+='<button class="btn btn-success btn-xs" type="button">'+data.data[i].class_name+'</button>';
			        result+='</div>';
			        result+='<div class="col-md-2 text-center"><a javascript:void(0) onclick="Common.openDetailPage('+data.data[i].topic_id+','+data.data[i].class_id+','+data.data[i].type+')" class="btn-link">';
			        result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button">';
			        result+='<i class="fa fa-external-link">'+data.data[i].forward_cnt+'转发 </i>';
			        result+='</button></a></div>';
			        result+='<div class="col-md-2 text-center "><a javascript:void(0) onclick="Common.openDetailPage('+data.data[i].topic_id+','+data.data[i].class_id+','+data.data[i].type+')" class="btn-link">';
			        result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button" > ';
			        result+='<i class="fa fa-commenting-o">'+data.data[i].comment_cnt+'评论</i>   ';
			        result+='</button></a></div>';
			        result+='<div class="col-md-2 text-center " >'; 
			        if(data.data[i].is_thumbs==0){//用户已经点赞
			        	result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button"><i class="fa fa-thumbs-o-up" style="color:red">'+data.data[i].thumbs_cnt+'点赞 </i>  '; 
			        }else{//用户还未点赞
			        	result+='<button id="is_forward_btn" class="btn btn-white btn-xs" type="button" onclick="topicthumbsUp('+data.data[i].topic_id+',2)" ><i class="fa fa-thumbs-o-up"   >'+data.data[i].thumbs_cnt+'点赞 </i> ';
			        }
			        
			        result+='</button></div>';
			        result+='</div>';
			        result+='</div>';
			        result+='</div>';
				}
				$('#content').append(result);
				
					var result1='';
					var ret=0;
					if(type==1){
						//投票
						ret=Search.vote_cnt;
					}else if(type==2){
						//问卷
						ret=Search.surver_cnt;
					}else if(type==3){
						//活动
						ret=Search.activities_cnt;
					}
					if(ret>$('#content').children().length){
						result1+='<button class="btn btn-primary btn-block m " id="get_data_button" onclick="loadTopicData('+type+')"><i class="fa fa-arrow-down"></i> 显示更多</button>';
					}else{
						result1+='<button class="btn btn-primary btn-block m"> 没有更多消息</button>';
					}
					$('#content').append(result1);
				}
			},
			error:function(data){
			}
	    });

}

